package com.uha.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uha.reggie.DTO.DishDto;
import com.uha.reggie.entity.Dish;
import com.uha.reggie.entity.DishFlavor;
import com.uha.reggie.mapper.DishMapper;
import com.uha.reggie.service.DishFlavorService;
import com.uha.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author uha
 * @date 2023/6/15
 * @apiNote
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * 涉及到多张表
     * @param dishDto
     */


    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {

        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //菜品Id
        Long dishId = dishDto.getId();
        //菜品口味 因为封装的口味信息缺少dishId 所以：

        List<DishFlavor> flavors = dishDto.getFlavors();
                    flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味信息到菜品口味表dish_flavor

        dishFlavorService.saveBatch(flavors);



    }

    /**
     * 根据id查询数据回显
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        //查询菜品的基本信息

        Dish dish = this.getById(id);

        //对象拷贝
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());

        List<DishFlavor> flavors = dishFlavorService.list(wrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 更新菜品，同时保存对应的口味数据
     * 涉及到多张表
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //先更新菜品表

        this.updateById(dishDto);

        //菜品口味表 可以先删除 再添加
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味信息到菜品口味表dish_flavor

        dishFlavorService.saveBatch(flavors);


    }

    @Override
    @Transactional
    public void deleteWithFlavor(Long[] ids) {



        //根据dish_id批量删除dish_flavor

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();

        for(Long id:ids){
            queryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(queryWrapper);
        }

        //批量删除dish表
        this.removeByIds(Arrays.asList(ids));

    }


}
